String.prototype.trim = function 	() 
{
	return this.replace(/^\s*/, "").replace(/\s*$/, "").replace(" ","-");
}

function DatabaseAccess() {
	this.databaseFileName = null;
	this.dbConnection = new SQLite();
}

DatabaseAccess.prototype.createDatabase = databaseAccess_createDatabase;
DatabaseAccess.prototype.openDatabase = databaseAccess_openDatabase;
DatabaseAccess.prototype.closeDatabase = databaseAccess_closeDatabase;
DatabaseAccess.prototype.getPrefferedCategories = databaseAccess_getPrefered;
DatabaseAccess.prototype.updateCategory = databaseAccess_updatePrefered;
DatabaseAccess.prototype.getAddressByName = databaseAccess_getAddressByName;
DatabaseAccess.prototype.getAddressByWord = databaseAccess_getAddressByWord;

DatabaseAccess.prototype.categoriesTable = "categories";
DatabaseAccess.prototype.resourceTable = "resources";

function databaseAccess_createDatabase()
{
	//if the database does not exists then copy the database from the widget bundle
	if(filesystem.itemExists(system.widgetDataFolder + "/database") == false) {
		this.databaseFileName = widget.extractFile("Resources/database");
	} else {
		this.databaseFileName = system.widgetDataFolder + "/database";
	}
}

function databaseAccess_openDatabase() {
	try {
		this.dbConnection.open(this.databaseFileName);
		return true;
	} catch(exception) {
		return false;
	}
}

function databaseAccess_closeDatabase() {
	try {
		this.dbConnection.close();
		return true;
	} catch(exception) {
		return false;
	}
}

function databaseAccess_getPrefered(howMany)
{	var r;

	try {		
		r = this.dbConnection.query("SELECT categories.id , prefered, categories.name, readtime, vizualizations, address \
									FROM categories, rsssources WHERE parent != '0' and categories.id = rsssources.id");
	} catch(e) { 
		alert(e.errMsg);
	}
	var info = [];
	counter = 1;
	while ((x = r.getRow()) != null)
	{
		var vect = new Array(6); 
		vect[0] = x['id'];
		vect[1] = x['address'];
		vect[2] = x['name'];
		vect[3] = x['readtime'];
		vect[4] = x['vizualizations'];
		vect[5] = x['prefered'];
		info[counter] = vect;
		counter++;
	}
	maximum = 0;
	for (i = 1; i < counter; i++)
	{
		maximum += info[i][5];
	}

	var result = new Array(howMany);
	
	for (i = 1; i < (howMany + 1); i++)
	{
		var randomnumber = Math.floor(Math.random() * (maximum - 1) + 1) //sa nu returneze si zero
		var suma = 0;
		for (j = 1; j < counter; j++)
		{
			suma += info[j][5];
			if(randomnumber < suma )
			{
				result[i - 1] = info[j];
				break;
			}
		}
	}
	return result;
}

function databaseAccess_updatePrefered(id, preferedInc, readTimeInc, vizualizationsInc) //primeste id-ul subcategoriei
{
	try
	{
		this.dbConnection.query("UPDATE categories SET prefered = prefered + " + preferedInc + ", readtime = readtime + "
								 + readTimeInc + ", vizualizations = vizualizations + " + vizualizationsInc + " where id=\"" 
								 + id + "\"");
	}
	catch(e)
	{
		print(e.errMsg);
	}
}

function getAddress(id)
{
	var r = this.dbConnection.query("SELECT address FROM rsssources where id=\"" + id + "\"");
	x = r.getRow();
	
	return x['address'];
}

function databaseAccess_getAddressByName(name) 
{
	try {
		var query = this.dbConnection.query("SELECT id, address FROM rsssources WHERE name LIKE '%" + name + "%'");
		return process_query(query);
	} catch(e) {
		return null;
	}
}

function databaseAccess_getAddressByWord(word) //returneaza id,adresa daca exista un cuvant in lantul lexical care se aseamana cu cel dat
{
	try {
		query = this.dbConnection.query("SELECT categories.id, address FROM categories, rsssources WHERE words LIKE '%" + 
											word + "%' and parent!='0' and categories.id = rsssources.id order by prefered");
		return process_query(query);	
	} catch(e) {
		return null;
	}
}

function process_query(query) {
	result = new Array();
	do {
		item = new Array(2);
		item[0] = query.current()['id'];
		item[1] = query.current()['address'];
		result.push(item);
	}while(query.next());
	return result;
}